<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <el-form
        label-width="130px"
        :model="projectForm"
        style="padding-right: 30px;"
        :rules="subjectOpinionrules"
        ref="projectForm"
      >
        <el-row>
          <el-col :span="11" style="margin-top: 27px;">
            <el-form-item class="form-item" label="征求意见名称:" prop="title">
              <el-input v-model="projectForm.title" placeholder="请输入课题名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-col :span="10">
              <el-form-item class="form-item" label="征求部门 :" prop="supportDeptName">
                {{projectForm.supportDeptName}}
                <el-input v-model="projectForm.supportDeptName" placeholder="请输入征求部门" style="display: none"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                         @click="clickLevyDept">征集部门
              </el-button>
            </el-col>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item class="form-item" label="截止时间 :" prop="endDate" style="float: left;">
              <div>
                <el-date-picker
                  style="width:320px"
                  v-model="projectForm.endDate"
                  type="datetime"
                  placeholder="选择日期"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  :picker-options="expireTimeOption"
                >
                </el-date-picker>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item" label="征求意见描述 :" prop="txt">
              <el-input
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4}"
                placeholder="请输入课题概述"
                v-model="projectForm.txt"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="form-group form-padding" style="margin-bottom: 2rem;margin-top: 1rem;padding-left: 80px;">
        <span class="form-label-small" style="float: left;margin-right: -29px;font-weight: 600">附件 :</span>
        <el-upload
          style="margin-top: 6px;margin-left: 53px;"
          class="upload-demo"
          ref="uploadrelated"
          action="string"
          :http-request="submitUploadMixin"
          :on-preview="handlePreviewMixin"
          :on-remove="handleRemoveMixin"
          :file-list="fileListMixin"
          :auto-upload="true"
          :on-change="fileChangeMixin">
          <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
        </el-upload>
      </div>
      <div class="form-submit">
        <el-button type="primary" @click.stop="commitList()">发 送</el-button>
        <el-button type="info" @click.stop="goBack()">关 闭</el-button>
      </div>
      <xieorg-tree :orgCodeVisiblesubmit="levyDeptCodeVisiblesubmit" @orgCodeClick="levyDeptCodeClick"
                   @orgCodeHandleClosesubmit="levyDeptCodeHandleClosesubmit"
                   @orgCodeCheckChange="levyDeptCodeCheckChange"></xieorg-tree>
    </div>
  </basic-container>
</template>

<script>
  import {subjectOpinionrules} from "@/const/infoRules";
  import {addSubjectOPinion} from "@/api/subject";
  import relatedfile from "@/mixins/relatedfile";

  export default {
    name: "addSubjectOpinionPlan",
    mixins: [relatedfile],
    created() {
      this.projectForm.subjectId = this.$route.params.id;
      this.projectForm.type = '1';
    },
    data() {
      return {
        headerData: {
          title: '课题计划-征求意见-新增征求意见'
        },
        subjectOpinionrules: subjectOpinionrules,// 表单验证
        projectForm: {
          subjectId: 0,
          supportDeptName: '',
          deptIds: [],
          type: '',
          fileIds: [],
        },
        levyDeptCodeVisiblesubmit: false,//协同部门树弹窗标志
        expireTimeOption: {
          disabledDate(date) { //disabledDate 文档上：设置禁用状态，参数为当前日期，要求返回 Boolean
            return date.getTime() <= Date.now();
          }
        },
      }
    },
    methods: {
      // 弹出征集部门弹框
      clickLevyDept() {
        this.levyDeptCodeVisiblesubmit = true
      },
      levyDeptCodeClick() {
      },
      levyDeptCodeHandleClosesubmit() {
        this.levyDeptCodeVisiblesubmit = false
      },
      levyDeptCodeCheckChange(data) {
        this.projectForm.supportDeptName = '';
        this.projectForm.deptIds = [];
        for (var i in data) {
          this.projectForm.supportDeptName += data[i].name + ',';
          this.projectForm.deptIds.push(data[i].id);
        }
      },
      goBack() {
        this.$router.go(-1);
      },
      commitList() {
        if (this.projectForm.supportDeptName == '') {
          this.$notify({
            title: "提醒",
            type: "warning",
            message: "请选择征求部门",
            duration: 2000
          });
          return;
        }
        this.$refs.projectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定发布征求意见吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              //将多个相关材料的附件id拼成逗号隔开的字符串放在 this.projectForm.fileId 中
              var arr = [];
              for (var item in this.fileListMixin) {
                arr.push(this.fileListMixin[item].url);
              }
              this.projectForm.fileIds = arr;

              addSubjectOPinion(this.projectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "发布成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(-1);
                }
              })
            })
          }
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
  .form-submit {
    text-align: center;
    margin-top: 15px;
    margin-bottom: 15px;
  }

  .form-item {
    font-weight: 600;
  }
</style>
